package com.ruoyi.advertisement.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.advertisement.mapper.AdvertisementStrategyMapper;
import com.ruoyi.advertisement.domain.AdvertisementStrategy;
import com.ruoyi.advertisement.service.IAdvertisementStrategyService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;

/**
 * 广告投放策略Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class AdvertisementStrategyServiceImpl implements IAdvertisementStrategyService 
{
    @Autowired
    private AdvertisementStrategyMapper advertisementStrategyMapper;

    /**
     * 查询广告投放策略
     * 
     * @param strategyId 广告投放策略主键
     * @return 广告投放策略
     */
    @Override
    public AdvertisementStrategy selectAdvertisementStrategyByStrategyId(Long strategyId)
    {
        AdvertisementStrategy strategy = advertisementStrategyMapper.selectAdvertisementStrategyByStrategyId(strategyId);
        if (strategy != null) {
            // 查询关联的设备和素材
            List<String> deviceIds = advertisementStrategyMapper.selectDeviceIdsByStrategyId(strategyId);
            List<Long> materialIds = advertisementStrategyMapper.selectMaterialIdsByStrategyId(strategyId);
            strategy.setDeviceIds(deviceIds);
            strategy.setMaterialIds(materialIds);
        }
        return strategy;
    }

    /**
     * 查询广告投放策略列表
     * 
     * @param advertisementStrategy 广告投放策略
     * @return 广告投放策略
     */
    @Override
    public List<AdvertisementStrategy> selectAdvertisementStrategyList(AdvertisementStrategy advertisementStrategy)
    {
        return advertisementStrategyMapper.selectAdvertisementStrategyList(advertisementStrategy);
    }

    /**
     * 新增广告投放策略
     * 
     * @param advertisementStrategy 广告投放策略
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAdvertisementStrategy(AdvertisementStrategy advertisementStrategy)
    {
        advertisementStrategy.setCreateTime(DateUtils.getNowDate());
        advertisementStrategy.setCreateBy(SecurityUtils.getUsername());
        
        int result = advertisementStrategyMapper.insertAdvertisementStrategy(advertisementStrategy);
        
        // 插入设备关联
        if (advertisementStrategy.getDeviceIds() != null && !advertisementStrategy.getDeviceIds().isEmpty()) {
            advertisementStrategyMapper.batchInsertStrategyDevice(advertisementStrategy.getStrategyId(), 
                advertisementStrategy.getDeviceIds());
        }
        
        // 插入素材关联
        if (advertisementStrategy.getMaterialIds() != null && !advertisementStrategy.getMaterialIds().isEmpty()) {
            advertisementStrategyMapper.batchInsertStrategyMaterial(advertisementStrategy.getStrategyId(), 
                advertisementStrategy.getMaterialIds());
        }
        
        return result;
    }

    /**
     * 修改广告投放策略
     * 
     * @param advertisementStrategy 广告投放策略
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAdvertisementStrategy(AdvertisementStrategy advertisementStrategy)
    {
        advertisementStrategy.setUpdateTime(DateUtils.getNowDate());
        advertisementStrategy.setUpdateBy(SecurityUtils.getUsername());
        
        // 删除旧的关联关系
        advertisementStrategyMapper.deleteStrategyDeviceByStrategyId(advertisementStrategy.getStrategyId());
        advertisementStrategyMapper.deleteStrategyMaterialByStrategyId(advertisementStrategy.getStrategyId());
        
        // 插入新的设备关联
        if (advertisementStrategy.getDeviceIds() != null && !advertisementStrategy.getDeviceIds().isEmpty()) {
            advertisementStrategyMapper.batchInsertStrategyDevice(advertisementStrategy.getStrategyId(), 
                advertisementStrategy.getDeviceIds());
        }
        
        // 插入新的素材关联
        if (advertisementStrategy.getMaterialIds() != null && !advertisementStrategy.getMaterialIds().isEmpty()) {
            advertisementStrategyMapper.batchInsertStrategyMaterial(advertisementStrategy.getStrategyId(), 
                advertisementStrategy.getMaterialIds());
        }
        
        return advertisementStrategyMapper.updateAdvertisementStrategy(advertisementStrategy);
    }

    /**
     * 批量删除广告投放策略
     * 
     * @param strategyIds 需要删除的广告投放策略主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAdvertisementStrategyByStrategyIds(Long[] strategyIds)
    {
        // 删除关联关系
        for (Long strategyId : strategyIds) {
            advertisementStrategyMapper.deleteStrategyDeviceByStrategyId(strategyId);
            advertisementStrategyMapper.deleteStrategyMaterialByStrategyId(strategyId);
        }
        
        return advertisementStrategyMapper.deleteAdvertisementStrategyByStrategyIds(strategyIds);
    }

    /**
     * 删除广告投放策略信息
     * 
     * @param strategyId 广告投放策略主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAdvertisementStrategyByStrategyId(Long strategyId)
    {
        // 删除关联关系
        advertisementStrategyMapper.deleteStrategyDeviceByStrategyId(strategyId);
        advertisementStrategyMapper.deleteStrategyMaterialByStrategyId(strategyId);
        
        return advertisementStrategyMapper.deleteAdvertisementStrategyByStrategyId(strategyId);
    }

    /**
     * 根据设备ID查询广告策略
     * 
     * @param deviceId 设备ID
     * @return 广告策略
     */
    @Override
    public AdvertisementStrategy selectStrategyByDeviceId(String deviceId)
    {
        AdvertisementStrategy strategy = advertisementStrategyMapper.selectStrategyByDeviceId(deviceId);
        if (strategy != null) {
            // 查询关联的素材
            List<Long> materialIds = advertisementStrategyMapper.selectMaterialIdsByStrategyId(strategy.getStrategyId());
            strategy.setMaterialIds(materialIds);
        }
        return strategy;
    }

    /**
     * 查询启用状态的策略列表
     * 
     * @return 策略列表
     */
    @Override
    public List<AdvertisementStrategy> selectEnabledStrategies()
    {
        return advertisementStrategyMapper.selectEnabledStrategies();
    }

    /**
     * 启用/禁用策略
     * 
     * @param strategyId 策略ID
     * @param status 状态(0-禁用,1-启用)
     * @return 结果
     */
    @Override
    public int changeStatus(Long strategyId, Integer status)
    {
        AdvertisementStrategy strategy = new AdvertisementStrategy();
        strategy.setStrategyId(strategyId);
        strategy.setStatus(status);
        strategy.setUpdateTime(DateUtils.getNowDate());
        strategy.setUpdateBy(SecurityUtils.getUsername());
        return advertisementStrategyMapper.updateAdvertisementStrategy(strategy);
    }

    /**
     * 复制策略
     * 
     * @param strategyId 源策略ID
     * @param newStrategyName 新策略名称
     * @return 结果
     */
    @Override
    @Transactional
    public int copyStrategy(Long strategyId, String newStrategyName)
    {
        // 查询源策略
        AdvertisementStrategy sourceStrategy = selectAdvertisementStrategyByStrategyId(strategyId);
        if (sourceStrategy == null) {
            return 0;
        }
        
        // 创建新策略
        AdvertisementStrategy newStrategy = new AdvertisementStrategy();
        newStrategy.setStrategyName(newStrategyName);
        newStrategy.setForcedViewDuration(sourceStrategy.getForcedViewDuration());
        newStrategy.setSkipEnabled(sourceStrategy.getSkipEnabled());
        newStrategy.setRotationType(sourceStrategy.getRotationType());
        newStrategy.setStatus(0); // 新策略默认禁用
        newStrategy.setDeviceIds(sourceStrategy.getDeviceIds());
        newStrategy.setMaterialIds(sourceStrategy.getMaterialIds());
        
        return insertAdvertisementStrategy(newStrategy);
    }

    /**
     * 获取策略统计信息
     * 
     * @return 统计信息
     */
    @Override
    public StrategyStatistics getStrategyStatistics()
    {
        StrategyStatistics statistics = new StrategyStatistics();
        
        // 查询所有策略
        List<AdvertisementStrategy> allStrategies = advertisementStrategyMapper.selectAdvertisementStrategyList(new AdvertisementStrategy());
        
        int totalStrategies = allStrategies.size();
        int enabledStrategies = 0;
        int linkedDevices = 0;
        int linkedMaterials = 0;
        
        for (AdvertisementStrategy strategy : allStrategies) {
            if (strategy.getStatus() == 1) {
                enabledStrategies++;
            }
            
            // 统计关联的设备和素材数量
            List<String> deviceIds = advertisementStrategyMapper.selectDeviceIdsByStrategyId(strategy.getStrategyId());
            List<Long> materialIds = advertisementStrategyMapper.selectMaterialIdsByStrategyId(strategy.getStrategyId());
            
            if (deviceIds != null) {
                linkedDevices += deviceIds.size();
            }
            if (materialIds != null) {
                linkedMaterials += materialIds.size();
            }
        }
        
        statistics.setTotalStrategies(totalStrategies);
        statistics.setEnabledStrategies(enabledStrategies);
        statistics.setLinkedDevices(linkedDevices);
        statistics.setLinkedMaterials(linkedMaterials);
        
        return statistics;
    }
} 